﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Drawing;

namespace Home.Haushalt
{
    /// <summary>
    /// 
    /// </summary>
    public class Chips
    {

        #region FILE

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fullFilePath"></param>
        /// <returns></returns>
        public static bool FileTryDelete(string fullFilePath)
        {
            try
            {
                File.Delete(fullFilePath);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Einzelne Dateien und Verzeichnisse werden nach Schreibschutz geprüft.
        /// Schreibschutz wird aufgehoben. Die gesperrten Dateien werden fürs Löschen
        /// nach Neustart markiert.
        /// </summary>
        /// <param name="dirname">The dirname.</param>
        public static void DirectoriesAndFilesTryDelete(string dirname)
        {
            if (!Directory.Exists(dirname))
                return;

            //Dateien löschen
            foreach (string filename in Directory.GetFiles(dirname))
                try
                {
                    SetReadOnly(filename, false);

                    File.Delete(filename);
                }
                catch
                {
                }

            foreach (string folder in Directory.GetDirectories(dirname))
            {
                DirectoriesAndFilesTryDelete(folder);
            }

            //Verzeichnis löschen
            if (Directory.Exists(dirname))
                try
                {
                    Directory.Delete(dirname, true);
                }
                catch
                {
                }
        }

        /// <summary>
        ///  Read-Only-Attribut setzen
        /// </summary>
        /// <param name="filename">Dateiname</param>
        /// <param name="isreadonly">Gewünschtes Status</param>
        /// <returns>Status verändert</returns>
        public static bool SetReadOnly(string filename, bool isreadonly)
        {
            FileInfo f = new FileInfo(filename);
            if (((f.Attributes & FileAttributes.ReadOnly) != 0 && !isreadonly) ||
                ((f.Attributes & FileAttributes.ReadOnly) == 0 && isreadonly))
            {
                f.Attributes = f.Attributes ^ FileAttributes.ReadOnly;
                return true;
            }

            return false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="hidden"></param>
        /// <returns></returns>
        private static bool SetHidden(string filename, bool hidden)
        {
            FileInfo f = new FileInfo(filename);
            if (((f.Attributes & FileAttributes.Hidden) != 0 && !hidden) ||
                ((f.Attributes & FileAttributes.Hidden) == 0 && hidden))
            {
                f.Attributes = f.Attributes ^ FileAttributes.Hidden;
                return true;
            }

            return false;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="system"></param>
        /// <returns></returns>
        private static bool SetSystem(string filename, bool system)
        {
            FileInfo f = new FileInfo(filename);
            if (((f.Attributes & FileAttributes.System) != 0 && !system) ||
                ((f.Attributes & FileAttributes.System) == 0 && system))
            {
                f.Attributes = f.Attributes ^ FileAttributes.System;
                return true;
            }

            return false;
        }

        #endregion FILE

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static bool ShowExplorerAtFile(string filename)
        {
            if (File.Exists(filename))
            {
                Process explorerResultProcess = new Process();
                explorerResultProcess.StartInfo.UseShellExecute = true;
                explorerResultProcess.StartInfo.FileName = "explorer.exe";
                explorerResultProcess.StartInfo.Arguments = string.Format("/e,/select,\"{0}\"", filename);
                explorerResultProcess.Start();

                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="foldername"></param>
        public static void ShowExplorerAtFolder(string foldername)
        {
            Process.Start(foldername);
        }

        /// <summary>
        /// <example> Dictionary<string, string> startArgs =
        ///            Chips.GetStartArgs(
        ///            OPTIONNAME_SCRIPTDEPLOY + "|false",
        ///            OPTIONNAME_SETUPFILE,
        ///            OPTIONNAME_CREATESETUP + "|false");</example>
        /// </summary>
        /// <param name="defaults"></param>
        /// <returns></returns>
        public static Dictionary<string, string> GetStartArgs(params string[] defaults)
        {
            Dictionary<string, string> lineStartArgs = new Dictionary<string, string>();

            //Voreinstellungen initialisieren
            foreach (string defa in defaults)
            {
                string[] parts = defa.Split('|');
                if (parts.Length > 1)
                    lineStartArgs.Add(parts[0], parts[1]);
                else
                    lineStartArgs.Add(parts[0], "");
            }

            string[] argsfromzip = Environment.GetCommandLineArgs();

            //Parameter zusammenführen um die Leerzeichen in der Parameter zu erhalten.
            //Nur ein Leerzeichen wird unterstützt.
            string argstring = "";
            foreach (string argfromzip in argsfromzip)
            {
                argstring += argfromzip + " ";
            }

            try
            {
                //Parameter wieder trennen und innitialisieren
                foreach (string arg in argstring.Split('/'))
                {
                    string optOrigin = arg.Trim();
                    string opt = optOrigin.ToLower();

                    if (optOrigin.Length > 0)
                    {
                        string[] parts = optOrigin.Split('=');

                        string optionName = parts[0].ToLower();

                        if (parts.Length > 1)
                        {
                            string optionValue = parts[1];

                            if (!lineStartArgs.ContainsKey(optionName))
                            {
                                lineStartArgs.Add(optionName, optionValue);
                            }
                            else
                            {
                                lineStartArgs[optionName] = optionValue;
                            }
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                Journal.Add(typeof(Chips), ActionResult.Hinweis, "Start-Args konnten nicht (vollständig) ausgelesen werden", ex);
            }

            return lineStartArgs;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="bitmaps"></param>
        /// <returns></returns>
        public static Bitmap CombineBitmaps(Bitmap[] bitmaps)
        {
            if (bitmaps.Length == 0)
                return new Bitmap(1, 1);
            int width = 0;
            int height = 0;
            foreach (Bitmap bmp in bitmaps)
            {
                width += bmp.Width;
                if (height < bmp.Height)
                    height = bmp.Height;
            }
            Bitmap bitmap = new Bitmap(width, height);
            using (Graphics grfx = Graphics.FromImage(bitmap))
            {
                int x = 0;
                foreach (Bitmap bmp in bitmaps)
                {
                    grfx.DrawImage(bmp, x, 0, bmp.Width, height);
                    x += bmp.Width;
                }
            }
            return bitmap;
        }
    }
}
